<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/js/toastr/toastr.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/toastr/toastr.css">

</head>

<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->
<div class="box-header with-border">
    <h3 class="box-title">规格管理</h3>
</div>
<div class="box-body">
    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#add"><i
                            class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" title="删除" onclick="deleteInfo()"><i
                            class="fa fa-trash-o"></i> 删除
                    </button>

                    <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i
                            class="fa fa-refresh"></i> 刷新
                    </button>
                </div>
            </div>
        </div>
        <div class="box-tools pull-right">
            <div class="has-feedback">
                规格名称：<input type="text" name="specName" id="specName">
                <button class="btn btn-default" onclick="show()">查询</button>
            </div>
        </div>
        <!--工具栏/-->
        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">规格ID</th>
                <th class="sorting">规格名称</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <script>
                $(function () {
                    show();
                })

                function show() {
                    $.ajax({
                        url: "/specification/getSpecification",
                        data: {
                            specName: $("#specName").val(),
                            pageNum: $("#pageNumber").val(),
                            pageSize: $("#pageSize").val()
                        },
                        type: "get", //请求方式 get post 默认get
                        dataType: "json", //返回值类型 html json text xml
                        async: true,//true代表异步请求 false代表同步请求
                        success: function (result) {//成功回调函数
                            var page = result.data.rows;
                            var div = "";
                            for (var i = 0; i < page.length; i++) {
                                div += '<tr>';
                                div += '<td><input type="checkbox" name="check" value="' + page[i].id + '"></td>';
                                div += '<td>' + page[i].id + '</td>';
                                div += '<td>' + page[i].specName + '</td>';
                                div += '<td className="text-center">';
                                div += '<button type="button" className="btn bg-olive btn-xs"data-toggle="modal" data-target="#update" onclick="toupdate(' + page[i].id + ')">修改 </button>';
                                div += '</td>';
                                div += '</tr>';
                            }
                            $("#id").html(div);
                            install_page(result.data)
                        },
                        error: function (i) {//发生错误回调函数
                            alert("异常错误")
                        }

                    })
                }

                function deleteInfo() {
                    var rows = $("[name=check]:checked");
                    var ids = [];
                    $(rows).each(function (i, e) {
                        ids[i] = e.value;
                    })
                    if (ids.length == 0) {
                        toastr.success("请选择要删除的数据");
                        return;
                    }
                    $.ajax({
                        url: "/specification/deleteInfo/" + ids,
                        type: "get", //请求方式 get post 默认get
                        dataType: "json", //返回值类型 html json text xml
                        async: true,//true代表异步请求 false代表同步请求
                        success: function (result) {//成功回调函数
                            if (result.code == 10000) {
                                toastr.success("删除成功");
                                window.location.reload();
                            } else {
                                toastr.error("删除失败");
                            }

                        },
                        error: function (i) {//发生错误回调函数
                            alert("异常错误")
                        }
                    })
                }

                function toupdate(id) {
                    $.ajax({
                        url: "/specification/toUpdate/" + id,
                        type: "get", //请求方式 get post 默认get
                        dataType: "json", //返回值类型 html json text xml
                        async: true,//true代表异步请求 false代表同步请求
                        success: function (result) {//成功回调函数
                            var t = result.data.optionList;
                            var count = t.length - 3;
                            if (count > 0){//判断返回来的数据默认的三个表格够不够
                                for (var i = 0; i < count; i++) {
                                    addOptions();
                                }
                            }
                            $("#specName2").val(result.data.spec.specName);
                            $("#id1").val(id);
                            var trList = $("#options1").find("tr");
                            for (var i = 1; i < trList.length; i++) {//i=0时 tr的行时标题行,可以直接跳过去
                                var tdList = $(trList[i]).find("td");
                                $(tdList[0]).find("input").val(t[i - 1].optionName);
                                $(tdList[1]).find("input").val(t[i - 1].orders);
                            }
                        },
                        error: function (i) {//发生错误回调函数
                            alert("异常错误")
                        }
                    })
                }
            </script>
            <tbody id="id">

            </tbody>
        </table>
        <div th:insert="common/page.html"></div>
        <!--数据列表/-->
    </div>
    <!-- 数据表格 /-->
</div>
<!-- /.box-body -->
<script>

    function add() {
        var optionsLists = [];
        var trList = $("#options").find("tr");
        for (var i = 1; i < trList.length; i++) {//i=0时 tr的行时标题行,可以直接跳过去
            var tdList = $(trList[i]).find("td");
            var optionName = $(tdList[0]).find("input").val();
            var orders = $(tdList[1]).find("input").val();
            var optionJson = {optionName: optionName, orders: orders}
            optionsLists.push(optionJson);
        }
        //转换为字符串
        var options = JSON.stringify(optionsLists);
        $.ajax({
            url: "/specification/addInfo",
            type: "get", //请求方式 get post 默认get
            data: {specName: $("#specName1").val(), options: options},
            dataType: "json", //返回值类型 html json text xml
            async: true,//true代表异步请求 false代表同步请求
            success: function (result) {//成功回调函数
                if (result.code == 10000) {
                   toastr.success("增加成功");
                    setTimeout("refresh()", 2000);//2秒后执行refresh()
                }else {
                    toastr.error("增加失败");
                    setTimeout("refresh()", 2000);//2秒后执行refresh()
                }
            },
            error: function (i) {//发生错误回调函数
                alert("异常错误")
            }
        })
    }

    function updateInfo() {
        var optionsLists = [];
        var trList = $("#options1").find("tr");
        for (var i = 1; i < trList.length; i++) {//i=0时 tr的行时标题行,可以直接跳过去
            var tdList = $(trList[i]).find("td");
            var optionName = $(tdList[0]).find("input").val();
            var orders = $(tdList[1]).find("input").val();
            var optionJson = {optionName: optionName, orders: orders}
            optionsLists.push(optionJson);
        }
        //转换为字符串
        var options = JSON.stringify(optionsLists);
        $.ajax({
            url: "/specification/updateInfo",
            type: "get", //请求方式 get post 默认get
            data: {id: $("#id1").val(), specName: $("#specName2").val(), options: options},
            dataType: "json", //返回值类型 html json text xml
            async: true,//true代表异步请求 false代表同步请求
            success: function (result) {//成功回调函数
                if (result.code == 10000) {
                   toastr.success(result.message);
                    setTimeout("refresh()", 1000);//2秒后执行refresh()
                }else{
                    toastr.error("修改失败");
                    setTimeout("refresh()", 2000);//2秒后执行refresh()
                }
            },
            error: function (i) {//发生错误回调函数
                alert("异常错误")
            }
        })
    }


</script>

<!-- 编辑窗口 -->
<div class="modal fade" id="update" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" onclick="refresh()">×</button>
                <h3 id="myModalLabel1">规格编辑</h3>
            </div>
            <div class="modal-body">
                <table class="table table-bordered table-striped" width="800px">
                    <tr>
                        <td>规格名称</td>
                        <td><input class="form-control" placeholder="规格名称" name="specName" id="specName2"></td>
                        <input type="hidden" name="id" id="id1">
                    </tr>
                </table>

                <!-- 规格选项 -->
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" onclick="addOptions()"><i class="fa fa-file-o"></i> 新增规格选项
                    </button>

                </div>
                <table class="table table-bordered table-striped table-hover dataTable" id="options1">
                    <thead>
                    <tr>
                        <th class="sorting">规格选项</th>
                        <th class="sorting">排序</th>
                        <th class="sorting">操作</th>
                    </thead>
                    <tbody id="option0">

                    <tr>
                        <td>
                            <input class="form-control" name="optionName" placeholder="规格选项">
                        </td>
                        <td>
                            <input class="form-control" name="orders" placeholder="排序">
                        </td>
                        <td>
                            <button type="button" class="btn btn-default" title="删除" onclick="deleteOneTr(this)"><i class="fa fa-trash-o"></i>
                                删除
                            </button>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input class="form-control" name="optionName" placeholder="规格选项">
                        </td>
                        <td>
                            <input class="form-control" name="orders" placeholder="排序">
                        </td>
                        <td>
                            <button type="button" class="btn btn-default" title="删除" onclick="deleteOneTr(this)"><i class="fa fa-trash-o"></i>
                                删除
                            </button>
                        </td>
                    </tr>
                    <tr>

                        <td>
                            <input class="form-control" name="optionName" placeholder="规格选项">
                        </td>
                        <td>
                            <input class="form-control" name="orders" placeholder="排序">
                        </td>
                        <td>
                            <button type="button" class="btn btn-default" title="删除" onclick="deleteOneTr(this)"><i class="fa fa-trash-o"></i>
                                删除
                            </button>
                        </td>
                    </tr>

                    </tbody>

                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateInfo()">保存
                </button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true" onclick="refresh()">关闭</button>
            </div>
        </div>
    </div>
</div>
<script>
    //刷新函数
    function refresh() {
        window.location.reload();
    }
</script>
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">规格编辑</h3>
            </div>
            <div class="modal-body">
                <form id="formID">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>规格名称</td>
                            <td><input class="form-control" placeholder="规格名称" name="specName" id="specName1"></td>
                        </tr>
                    </table>

                    <!-- 规格选项 -->
                    <div class="btn-group">
                        <button type="button" class="btn btn-default" title="新建" onclick="addOptions1()"><i class="fa fa-file-o"></i> 新增规格选项
                        </button>

                    </div>
                    <table class="table table-bordered table-striped table-hover dataTable" id="options">
                        <thead>
                        <tr>
                            <th class="sorting">规格选项</th>
                            <th class="sorting">排序</th>
                            <th class="sorting">操作</th>
                        </thead>
                        <tbody id="option1">

                        <tr>
                            <td>
                                <input class="form-control" name="optionName" placeholder="规格选项">
                            </td>
                            <td>
                                <input class="form-control" name="orders" placeholder="排序">
                            </td>
                            <td>
                                <button type="button" class="btn btn-default" title="删除" onclick="deleteOneTr1(this)"><i class="fa fa-trash-o"></i>
                                    删除
                                </button>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input class="form-control" name="optionName" placeholder="规格选项">
                            </td>
                            <td>
                                <input class="form-control" name="orders" placeholder="排序">
                            </td>
                            <td>
                                <button type="button" class="btn btn-default" title="删除" onclick="deleteOneTr1(this)"><i class="fa fa-trash-o"></i>
                                    删除
                                </button>
                            </td>
                        </tr>
                        <tr>

                            <td>
                                <input class="form-control" name="optionName" placeholder="规格选项">
                            </td>
                            <td>
                                <input class="form-control" name="orders" placeholder="排序">
                            </td>
                            <td>
                                <button type="button" class="btn btn-default" title="删除" onclick="deleteOneTr1(this)"><i class="fa fa-trash-o"></i>
                                    删除
                                </button>
                            </td>
                        </tr>

                        </tbody>

                    </table>
                </form>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

</body>

</html>
<script type="text/javascript">
    function addOptions() {   //增加一行
        var a = document.getElementById("option0"); //定位在tbody 拿出值
        // var a = document.getElementById("option1"); //定位在tbody 拿出值
        var tr = document.createElement("tr");   //创建一个行<tr>
        tr.innerHTML = '<td><input style="width: 100%;height: 100%" className="form-control" name="optionName" placeholder="规格选项"></td><td><input style="width: 100%;height: 100%" className="form-control" name="orders" placeholder="排序"></td><td><button type="button" className="btn btn-default" title="删除" onclick="deleteOneTr(this)"><i className="fa fa-trash-o"></i>删除</button></td>';
        //上面可能会有一点点乱希望大家仔细看看
        a.appendChild(tr);	//将创建的<tr>加入到定位的tbody下
    }
    function deleteOneTr(obj){ //删除当前行(这里我就不写 script 这个格式了)
        var a = document.getElementById("option0"); //一样 需要找到 表格的父类
        // var a = document.getElementById("option1"); //一样 需要找到 表格的父类
        var a1 = obj.parentNode.parentNode;  // 找到需要删除的位置 ，当按钮触发时直接传入自己进来 这样就拿到了位置
        a.removeChild(a1);  // 直接 remove 这个子类
    }

    function addOptions1() {   //增加一行
        var a = document.getElementById("option1"); //定位在tbody 拿出值
        var tr = document.createElement("tr");   //创建一个行<tr>
        tr.innerHTML = '<td><input style="width: 100%;height: 100%" className="form-control" name="optionName" placeholder="规格选项"></td><td><input  style="width: 100%;height: 100%" className="form-control" name="orders" placeholder="排序"></td><td><button type="button" className="btn btn-default" title="删除" onclick="deleteOneTr1(this)"><i className="fa fa-trash-o"></i>删除</button></td>';
        //上面可能会有一点点乱希望大家仔细看看
        a.appendChild(tr);	//将创建的<tr>加入到定位的tbody下
    }
    function deleteOneTr1(obj){ //删除当前行(这里我就不写 script 这个格式了)
        var a = document.getElementById("option1"); //一样 需要找到 表格的父类
        var a1 = obj.parentNode.parentNode;  // 找到需要删除的位置 ，当按钮触发时直接传入自己进来 这样就拿到了位置
        a.removeChild(a1);  // 直接 remove 这个子类
    }

</script>